define([],function () {
    var Style = {

        themeLayerStyle: function (type,  feature, styleData) {
            var self = this;
            switch (type){
                case "uniquevalue":{
                    return self.uniqueValueStyle(feature, styleData);
                }
                case "breakvalue":{
                    return self.breakValueStyle(feature, styleData);
                }
                case "dotdensity":{
                    return self.dotDensityStyle(feature, styleData);
                }
            }
        },

        dotDensityStyle: function (feature, styleData) {
            var label = styleData.label,
                styleJson = {};

            var stroke = styleData.stroke? new ol.style.Stroke({
                    color: styleData.stroke.color? styleData.stroke.color: "red",
                    lineDash: [0],
                    width: styleData.stroke.width? styleData.stroke.width: 1
                }): null;
            var radius = styleData.radius? styleData.radius: 4;
            styleJson = {
                image: new ol.style.Circle({
                    radius: radius,
                    fill: new ol.style.Fill({
                        color: styleData.fill? styleData.fill: "red"
                    }),
                    stroke:stroke
                })
            };

            if(label.show) {
                var text = feature.get(label.field);
                var font = label.font? label.font: "normal 12px 微软雅黑";
                styleJson.text = new ol.style.Text({
                    font: font,
                    text: text.toString(),
                    textAlign:"left",
                    textBaseline:"middle",
                    offsetX: radius + 5 ,
                    fill: new ol.style.Fill({
                        color: '#000'
                    }),
                    stroke: new ol.style.Stroke({
                        color: 'white',
                        lineDash: [0],
                        width: 2
                    })
                })
            }
            return new ol.style.Style(styleJson);
        },

        /**
         * 断点值渲染
         * @param feature
         * @param styleData
         * @returns {*|Style}
         */
        breakValueStyle: function (feature, styleData) {
            var geom = feature.getGeometry();
            var field = styleData.field,
                value = parseFloat(feature.get(field)),
                colorMap = styleData.colorMap,
                label = styleData.label,
                stroke = styleData.stroke;

            var type = geom.getType().toLowerCase(),
                styleJson = {},
                color = "";

            for(var item in colorMap){
                var min = 0, max = 0;
                if(item.indexOf("<")!=-1){
                    min = -999999999;
                    max = parseFloat(item.split("<")[1]);
                } else if(item.indexOf(">")!=-1){
                    min = parseFloat(item.split(">")[1]);
                    max = 999999999;
                } else {

                    var range = item.split("-");
                    min = parseFloat(range[0]);
                    max = parseFloat(range[1]);
                }
                if(value>=min && value<max){
                    color = colorMap[item];
                    break;
                }
            }

            if(type.indexOf("polygon")!=-1){//面样式
                styleJson = {
                    fill: new ol.style.Fill({
                        color: color
                    }),
                    stroke: new ol.style.Stroke({
                        color: stroke.color,
                        lineDash: stroke.dash,
                        width: stroke.width
                    })
                }
            } else if((type.indexOf("linestring")!=-1)){
                styleJson = {
                    stroke: new ol.style.Stroke({
                        color: color,
                        lineDash: stroke.dash,
                        width: stroke.width
                    })
                }
            } else{
                styleJson = {
                    image: new ol.style.Circle({
                        radius: 8,
                        fill: new ol.style.Fill({
                            color: color
                        }),
                        stroke: new ol.style.Stroke({
                            color: stroke.color,
                            lineDash: stroke.dash,
                            width: stroke.width
                        })
                    })
                }
            }
            if(label.show) {
                var text = feature.get(label.field)+"("+value+")";
                var font = label.font? label.font: "normal 12px 微软雅黑";
                styleJson.text = new ol.style.Text({
                    font: font,
                    text: text.toString(),
                    fill: new ol.style.Fill({
                        color: label.textColor
                    }),
                    stroke: new ol.style.Stroke({
                        color: label.strokeColor,
                        width: label.strokeWidth
                    })
                })
            }
            return new ol.style.Style(styleJson);
        },

        /**
         * 唯一值渲染样式
         * @param feature
         * @param styleData
         * @returns {*|Style}
         */
        uniqueValueStyle: function (feature, styleData) {
            var geom = feature.getGeometry();
            var field = styleData.field,
                colorMap = styleData.colorMap,
                label = styleData.label,
                stroke = styleData.stroke;

            var type = geom.getType().toLowerCase(),
                styleJson = {},
                color = colorMap[feature.get(field)];

            if(type.indexOf("polygon")!=-1){//面样式
                styleJson = {
                    fill: new ol.style.Fill({
                        color: color
                    }),
                    stroke: new ol.style.Stroke({
                        color: stroke.color,
                        lineDash: stroke.dash,
                        width: stroke.width
                    })
                }
            } else if((type.indexOf("linestring")!=-1)){
                styleJson = {
                    stroke: new ol.style.Stroke({
                        color: color,
                        lineDash: stroke.dash,
                        width: stroke.width
                    })
                }
            } else{
                styleJson = {
                    image: new ol.style.Circle({
                        radius: 8,
                        fill: new ol.style.Fill({
                            color: color
                        }),
                        stroke: new ol.style.Stroke({
                            color: stroke.color,
                            lineDash: stroke.dash,
                            width: stroke.width
                        })
                    })
                }
            }
            if(label.show) {
                var text = feature.get(label.field);
                var font = label.font? label.font: "normal 12px 微软雅黑";
                styleJson.text = new ol.style.Text({
                    font: font,
                    text: text.toString(),
                    fill: new ol.style.Fill({
                        color: label.textColor
                    }),
                    stroke: new ol.style.Stroke({
                        color: label.strokeColor,
                        width: label.strokeWidth
                    })
                })
            }
            return new ol.style.Style(styleJson);
        }
    };

    return Style;
});